package gui;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.JPanel;

public class Freihandzeichnen extends JPanel implements MouseListener, MouseMotionListener { 

    /**
	 * 
	 */
	private static final long serialVersionUID = 6242584825581927445L;

    private ArrayList<ArrayList<Point>> pointsammlung = new ArrayList<ArrayList<Point>>();
    private ArrayList<Point> points = new ArrayList<Point>();

    public Freihandzeichnen() { 
        this.addMouseListener(this); 
        this.addMouseMotionListener(this); 
    } 
    
    public void clear(){
    	pointsammlung = new ArrayList<ArrayList<Point>>();
    	points = new ArrayList<Point>();
    	repaint(); 
    }

    protected void paintComponent(Graphics g) {
    	super.paintComponent(g);
    	if(pointsammlung.size()<1){
    		for(int i=0;i<points.size()-2;i++){
        		Point p1 = points.get(i);
        		Point p2 = points.get(i+1);
        		g.drawLine(p1.x, p1.y, p2.x, p2.y);
        	}
    	}else{
    	for (ArrayList<Point> pointss : pointsammlung) {
    		for(int i=0;i<pointss.size()-2;i++){
        		Point p1 = pointss.get(i);
        		Point p2 = pointss.get(i+1);
        		g.drawLine(p1.x, p1.y, p2.x, p2.y);
        	}
    		for(int i=0;i<points.size()-2;i++){
        		Point p1 = points.get(i);
        		Point p2 = points.get(i+1);
        		g.drawLine(p1.x, p1.y, p2.x, p2.y);
        	}
		}
    	}
  
    } 
     

    public void mouseClicked(MouseEvent e) { 
    
    } 

    public void mouseEntered(MouseEvent e) { 
    } 

    public void mouseExited(MouseEvent e) { 	
    } 

    public void mousePressed(MouseEvent e) {
    	if(points.size()>1){
    	pointsammlung.add(points);
    	points = new ArrayList<Point>();
    	}
    } 

    public void mouseReleased(MouseEvent e) { 
    } 

    public void mouseDragged(MouseEvent e) { 
    	points.add(e.getPoint());
        repaint(); 
    } 

    public void mouseMoved(MouseEvent e) { 
    } 
}  